package cn.dao789.mvc;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import cn.dao789.R;
import cn.dao789.model.UserModel;

/**
 * MVC架构中的View和Controller结合体
 * 在Android的MVC中，Activity通常同时扮演View和Controller的角色
 */
public class MvcActivity extends AppCompatActivity {
    // View组件
    private EditText editTextUsername;
    private EditText editTextEmail;
    private Button buttonSave;
    private TextView textViewUserInfo;
    
    // Model
    private UserModel userModel;
    // Controller
    private UserController userController;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mvc);
        
        // 初始化View组件
        editTextUsername = findViewById(R.id.editTextUsername);
        editTextEmail = findViewById(R.id.editTextEmail);
        buttonSave = findViewById(R.id.buttonSave);
        textViewUserInfo = findViewById(R.id.textViewUserInfo);
        
        // 初始化Model和Controller
        userModel = new UserModel("1", "默认用户", "default@example.com");
        userController = new UserController(userModel, this);
        
        // 更新UI显示初始数据
        updateUserInfo();
        
        // 设置事件监听（Controller的职责）
        buttonSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                userController.saveUser(editTextUsername.getText().toString(),
                        editTextEmail.getText().toString());
            }
        });
    }
    
    // 更新UI的方法（View的职责）
    public void updateUserInfo() {
        textViewUserInfo.setText("用户信息：\n" +
                "ID: " + userModel.getUserId() + "\n" +
                "用户名: " + userModel.getUsername() + "\n" +
                "邮箱: " + userModel.getEmail());
    }
}